package com.example.smartwaterapi.common.api;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 统一API响应结果封装
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResponseResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    private long code;

    /**
     * 返回信息
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;

    /**
     * 时间戳
     */
    private long timestamp;

    /**
     * 请求路径
     */
    private String path;

    /**
     * 是否成功
     */
    private boolean success;

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    @SuppressWarnings("unchecked")
    public static <T> ResponseResult<T> success(T data) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        result.setData(data);
        result.setTimestamp(System.currentTimeMillis());
        result.setSuccess(true);
        return result;
    }

    /**
     * 成功返回结果
     *
     * @param data    获取的数据
     * @param message 提示信息
     */
    @SuppressWarnings("unchecked")
    public static <T> ResponseResult<T> success(T data, String message) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(message);
        result.setData(data);
        result.setTimestamp(System.currentTimeMillis());
        result.setSuccess(true);
        return result;
    }

    /**
     * 失败返回结果
     *
     * @param errorCode 错误码
     */
    @SuppressWarnings("unchecked")
    public static <T> ResponseResult<T> failed(IErrorCode errorCode) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(errorCode.getCode());
        result.setMessage(errorCode.getMessage());
        result.setTimestamp(System.currentTimeMillis());
        result.setSuccess(false);
        return result;
    }

    /**
     * 失败返回结果
     *
     * @param errorCode 错误码
     * @param message   错误信息
     */
    @SuppressWarnings("unchecked")
    public static <T> ResponseResult<T> failed(IErrorCode errorCode, String message) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(errorCode.getCode());
        result.setMessage(message);
        result.setTimestamp(System.currentTimeMillis());
        result.setSuccess(false);
        return result;
    }

    /**
     * 失败返回结果
     *
     * @param message 提示信息
     */
    @SuppressWarnings("unchecked")
    public static <T> ResponseResult<T> failed(String message) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(ResultCode.FAILED.getCode());
        result.setMessage(message);
        result.setTimestamp(System.currentTimeMillis());
        result.setSuccess(false);
        return result;
    }

    /**
     * 失败返回结果
     */
    public static <T> ResponseResult<T> failed() {
        return failed(ResultCode.FAILED);
    }

    /**
     * 参数验证失败返回结果
     */
    public static <T> ResponseResult<T> validateFailed() {
        return failed(ResultCode.VALIDATE_FAILED);
    }

    /**
     * 参数验证失败返回结果
     *
     * @param message 提示信息
     */
    @SuppressWarnings("unchecked")
    public static <T> ResponseResult<T> validateFailed(String message) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(ResultCode.VALIDATE_FAILED.getCode());
        result.setMessage(message);
        result.setTimestamp(System.currentTimeMillis());
        result.setSuccess(false);
        return result;
    }

    /**
     * 未登录返回结果
     */
    @SuppressWarnings("unchecked")
    public static <T> ResponseResult<T> unauthorized(T data) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(ResultCode.UNAUTHORIZED.getCode());
        result.setMessage(ResultCode.UNAUTHORIZED.getMessage());
        result.setData(data);
        result.setTimestamp(System.currentTimeMillis());
        result.setSuccess(false);
        return result;
    }

    /**
     * 未授权返回结果
     */
    @SuppressWarnings("unchecked")
    public static <T> ResponseResult<T> forbidden(T data) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(ResultCode.FORBIDDEN.getCode());
        result.setMessage(ResultCode.FORBIDDEN.getMessage());
        result.setData(data);
        result.setTimestamp(System.currentTimeMillis());
        result.setSuccess(false);
        return result;
    }

    /**
     * 设置请求路径
     */
    public ResponseResult<T> setPath(String path) {
        this.path = path;
        return this;
    }
} 